package com.example.myactspringboot;

import org.activiti.bpmn.model.*;
import org.activiti.bpmn.model.Process;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.time.LocalDateTime;
import java.util.*;

/**
 * @Create 2023-10-23 21:08
 * @Description
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyActSpringBootApplication.class)
public class IntermediateCatch {
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private TaskService taskService;
    @Autowired
    RuntimeService runtimeService;
    @Autowired
    IdentityService identityService;
    @Autowired
    HistoryService historyService;

    /**
     * 部署流程定义
     */
    @Test
    public void deploy() {
        Deployment deployment = repositoryService.createDeployment() // 创建部署
                .addClasspathResource("processes/TimerCatch.bpmn20.xml") // 加载流程资源文件
                .name("定时器测试") // 流程名称
                .deploy(); // 部署
        System.out.println("流程部署ID:" + deployment.getId());
        System.out.println("流程部署Name:" + deployment.getName());
    }

    /**
     * 启动流程实例
     */
    @Test
    public void start() {
//        ProcessInstance pi = runtimeService.startProcessInstanceByKey("multiInstance"); // 流程定义表的KEY字段值
        ProcessInstance pi = runtimeService.startProcessInstanceById("TimerCatch:2:340029"); // 流程定义表的KEY字段值
        System.out.println("流程实例ID:" + pi.getId());
        System.out.println("流程定义ID:" + pi.getProcessDefinitionId());
    }

    /**
     * 完成支付
     */
    @Test
    public void completeTask() throws InterruptedException {
        Map<String, Object> time = new HashMap<String, Object>();
/*        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionId("TimerCatch:2:340029")
                .singleResult();
        // 获取所有节点信息
        Process process = repositoryService.getBpmnModel(processDefinition.getId()).getProcesses().get(0);

        // 获取全部节点列表，包含子节点
        Collection<FlowElement> allElements = getAllElements(process.getFlowElements(), null);
        for (FlowElement allElement : allElements) {
            System.out.println(allElement);
        }*/
        time.put("sureTime", "2023-10-23T21:49:30");
        time.put("deliveryTime","2023-10-23T21:53:30" );
        taskService.complete("350002",time);
        Thread.sleep(30000);
    }
    public static Collection<FlowElement> getAllElements(Collection<FlowElement> flowElements, Collection<FlowElement> allElements) {
        allElements = allElements == null ? new ArrayList<>() : allElements;

        for (FlowElement flowElement : flowElements) {
            allElements.add(flowElement);
            if (flowElement instanceof SubProcess) {
                // 继续深入子流程，进一步获取子流程
                allElements =getAllElements(((SubProcess) flowElement).getFlowElements(), allElements);
            }
        }
        return allElements;
    }
}
